package com.example.myapplication.apter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.Garbagesorting.CategorypageActivity;
import com.example.myapplication.Garbagesorting.SearchActivity;
import com.example.myapplication.Garbagesorting.SortSearchActivity;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.SearchpicturegridBean;
import com.google.gson.Gson;


import java.util.List;

public class SearchpicturegridApter extends  RecyclerView.Adapter<SearchpicturegridApter.MyServer>{
    private Context context ;
    private List<SearchpicturegridBean.RowsBean> list;
    private View view;
    private Integer integer;

    public SearchpicturegridApter(Context context, List<SearchpicturegridBean.RowsBean> list,Integer integer) {
        this.context = context;
        this.list = list;
        this.integer = integer;

    }

    @NonNull
    @Override
    public SearchpicturegridApter.MyServer onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //填充试图
        view = LayoutInflater.from(context).inflate(R.layout.item_sever,null);
        return new SearchpicturegridApter.MyServer(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SearchpicturegridApter.MyServer holder, int position) {
        Glide.with(context).load(TokenBean.IP+list.get(position).getImgUrl()).into(holder.sever_tv_img);
        holder.sever_iv_text.setText(list.get(position).getName());
        //判断是点击分类进入
        if(context instanceof SortSearchActivity){
           holder.li_sever.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   Intent intent = new Intent(context, CategorypageActivity.class);
                   Gson gson = new Gson();
                   String garbagesortingBeanName   = gson.toJson(list.get(position));
                   intent.putExtra("GarbagesortingBean",garbagesortingBeanName);
                   context.startActivity(intent);
               }
           });
        }
    }

    @Override
    public int getItemCount() {
        return integer;
    }

    class  MyServer extends RecyclerView.ViewHolder{

        private final RecyclerView recyclerView;
        private final TextView sever_iv_text;
        private final ImageView sever_tv_img;
        private final RelativeLayout li_sever;


        public MyServer(@NonNull View itemView) {
            super(itemView);
            recyclerView = itemView.findViewById(R.id.re_servlet);
            sever_iv_text = itemView.findViewById(R.id.sever_iv_text);
            sever_tv_img = itemView.findViewById(R.id.sever_tv_img);
            li_sever = itemView.findViewById(R.id.li_sever);
        }
    }
}
